
var util = require("util");
var http = require("../module/http");
var exception = require('./exception');

const api_host = "http://api.fangmm.com/api3/v3";
//const api_host = "http://alpha.fangmm.com/api3/v3";

var token_format = "bearer %s";
function buildHeader(token)
{
    if(token){
        var header = {};
        return header["Authorization"] = util.format(token_format,token);
    }
    return null;
}

var share_api_format = '%s/shares/brokerhouses/%s?from=wx';
function forShare() {

    this.houseId = '';

    this.call = function (token) {

        var header = buildHeader(token),
            url = util.format(share_api_format, api_host, this.houseId);
        return function(callback) {
            log.http('share url: %s', url);
            http.get(url, header, function(err, res, body) {
                if(err){
                    return callback(exception(exception.DBError, err.message));
                }
                log.http('share res: %s', body);
                callback(null, JSON.parse(body));
            })
        }
    }
}

exports.forShare = forShare;

